package com.ruoyi.caipiao.service.impl;

import java.util.*;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.caipiao.domain.CaipiaoPl5MethodThree;
import com.ruoyi.caipiao.utils.MapValueSortUtil;
import com.ruoyi.caipiao.utils.Pl5NumUtil;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.caipiao.mapper.CaipiaoPl5MethodThreeMapper;
import com.ruoyi.caipiao.service.ICaipiaoPl5MethodThreeService;

/**
 * 排列5方法3预测Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-07
 */
@Slf4j
@Service
public class CaipiaoPl5MethodThreeServiceImpl extends ServiceImpl<CaipiaoPl5MethodThreeMapper, CaipiaoPl5MethodThree> implements ICaipiaoPl5MethodThreeService {
    @Autowired
    private CaipiaoPl5MethodThreeMapper caipiaoPl5MethodThreeMapper;

    @Override
    public void generateCode(CaipiaoPl5MethodThree CaipiaoPl5MethodThree, ArrayList<CaipiaoPl5MethodThree> caipiaoPl5MethodThrees) {
        if (CaipiaoPl5MethodThree.getQs() == null) {
            throw new RuntimeException("请选择预测的期数!");
        }

        List<String> code = new ArrayList<>();
        Set<String> nums = Pl5NumUtil.getNums(300);
        for (; ; ) {
            nums.retainAll(Pl5NumUtil.getNums(300));
            if (nums.size() == 1) {
                code.addAll(nums);

                if (code.size() == 100) {
                    break;
                }

                nums = Pl5NumUtil.getNums(300);
            } else if (nums.isEmpty()) {
                nums = Pl5NumUtil.getNums(300);
            }
        }
        log.info("方法4：{}，{}" , nums.size(), nums.toString());

        caipiaoPl5MethodThrees.add(getCaipiaoPl5MethodThree(CaipiaoPl5MethodThree, code, 9));
        caipiaoPl5MethodThrees.add(getCaipiaoPl5MethodThree(CaipiaoPl5MethodThree, code, 8));
        caipiaoPl5MethodThrees.add(getCaipiaoPl5MethodThree(CaipiaoPl5MethodThree, code, 7));
        caipiaoPl5MethodThrees.add(getCaipiaoPl5MethodThree(CaipiaoPl5MethodThree, code, 6));
        caipiaoPl5MethodThrees.add(getCaipiaoPl5MethodThree(CaipiaoPl5MethodThree, code, 5));
        caipiaoPl5MethodThrees.add(getCaipiaoPl5MethodThree(CaipiaoPl5MethodThree, code, 4));
        caipiaoPl5MethodThrees.add(getCaipiaoPl5MethodThree(CaipiaoPl5MethodThree, code, 3));
        caipiaoPl5MethodThrees.add(getCaipiaoPl5MethodThree(CaipiaoPl5MethodThree, code, 2));
        caipiaoPl5MethodThrees.add(getCaipiaoPl5MethodThree(CaipiaoPl5MethodThree, code, 1));
//        caipiaoPl5MethodThreeMapper.insertCaipiaoPl5MethodThree(add9);
//        caipiaoPl5MethodThreeMapper.insertCaipiaoPl5MethodThree(add8);
//        caipiaoPl5MethodThreeMapper.insertCaipiaoPl5MethodThree(add7);
//        caipiaoPl5MethodThreeMapper.insertCaipiaoPl5MethodThree(add6);
//        caipiaoPl5MethodThreeMapper.insertCaipiaoPl5MethodThree(add5);
//        caipiaoPl5MethodThreeMapper.insertCaipiaoPl5MethodThree(add4);
//        caipiaoPl5MethodThreeMapper.insertCaipiaoPl5MethodThree(add3);
//        caipiaoPl5MethodThreeMapper.insertCaipiaoPl5MethodThree(add2);
//        caipiaoPl5MethodThreeMapper.insertCaipiaoPl5MethodThree(add1);
    }

    private CaipiaoPl5MethodThree getCaipiaoPl5MethodThree(CaipiaoPl5MethodThree CaipiaoPl5MethodThree, List<String> code, int size) {
        CaipiaoPl5MethodThree add = new CaipiaoPl5MethodThree();
        add.setId(IdUtil.getSnowflakeNextId());
        add.setCreateTime(new Date());
        add.setQs(CaipiaoPl5MethodThree.getQs());
        add.setNumSize(size);
        Set<String> p1 = getPositionCode(code, 0, size, "asc");
        Set<String> p2 = getPositionCode(code, 1, size, "asc");
        Set<String> p3 = getPositionCode(code, 2, size, "asc");
        Set<String> p4 = getPositionCode(code, 3, size, "asc");
        Set<String> p5 = getPositionCode(code, 4, size, "asc");
        add.setPosition1(String.join(",", p1));
        add.setPosition2(String.join(",", p2));
        add.setPosition3(String.join(",", p3));
        add.setPosition4(String.join(",", p4));
        add.setPosition5(String.join(",", p5));
        return add;
    }

    private Set<String> getPositionCode(List<String> code, int p, int size, String sortType) {
        Map<String, Integer> map = new HashMap<>();
        for (String s : code) {
            String n = s.substring(p, p + 1);
            map.merge(n, 1, Integer::sum);
        }
        return new TreeSet<>(MapValueSortUtil.sortMapByValue(map, sortType).subList(0, size));
    }

    @Override
    public List<CaipiaoPl5MethodThree> checkCode(CaipiaoPl5MethodThree CaipiaoPl5MethodThree) {
        List<CaipiaoPl5MethodThree> list = caipiaoPl5MethodThreeMapper.listNoCheckNum(CaipiaoPl5MethodThree);
        if (!list.isEmpty()) {
            for (CaipiaoPl5MethodThree one : list) {
                if (one.getKjNum() != null) {
                    String kjNum = one.getKjNum();
                    boolean zj = true;
                    boolean zjPl3 = true;
                    if (!one.getPosition1().contains(kjNum.substring(0, 1))) {
                        zj = false;
                        zjPl3 = false;
                    }
                    if (!one.getPosition2().contains(kjNum.substring(1, 2))) {
                        zj = false;
                        zjPl3 = false;
                    }
                    if (!one.getPosition3().contains(kjNum.substring(2, 3))) {
                        zj = false;
                        zjPl3 = false;
                    }
                    if (!one.getPosition4().contains(kjNum.substring(3, 4))) {
                        zj = false;
                    }
                    if (!one.getPosition5().contains(kjNum.substring(4, 5))) {
                        zj = false;
                    }
                    if (zj) {
                        one.setRightPl5(1);
                    } else {
                        one.setRightPl5(2);
                    }

                    if (zjPl3) {
                        one.setRightPl3(1);
                    } else {
                        one.setRightPl3(2);
                    }

                    caipiaoPl5MethodThreeMapper.updateCaipiaoPl5MethodThree(one);
                }
            }
        }
        return list;
    }

    /**
     * 查询排列5方法3预测
     *
     * @param id 排列5方法3预测主键
     * @return 排列5方法3预测
     */
    @Override
    public CaipiaoPl5MethodThree selectCaipiaoPl5MethodThreeById(Long id) {
        return caipiaoPl5MethodThreeMapper.selectCaipiaoPl5MethodThreeById(id);
    }

    /**
     * 查询排列5方法3预测列表
     *
     * @param caipiaoPl5MethodThree 排列5方法3预测
     * @return 排列5方法3预测
     */
    @Override
    public List<CaipiaoPl5MethodThree> selectCaipiaoPl5MethodThreeList(CaipiaoPl5MethodThree caipiaoPl5MethodThree) {
        return caipiaoPl5MethodThreeMapper.selectCaipiaoPl5MethodThreeList(caipiaoPl5MethodThree);
    }

    /**
     * 新增排列5方法3预测
     *
     * @param caipiaoPl5MethodThree 排列5方法3预测
     * @return 结果
     */
    @Override
    public int insertCaipiaoPl5MethodThree(CaipiaoPl5MethodThree caipiaoPl5MethodThree) {
        caipiaoPl5MethodThree.setCreateTime(DateUtils.getNowDate());
        return caipiaoPl5MethodThreeMapper.insertCaipiaoPl5MethodThree(caipiaoPl5MethodThree);
    }

    /**
     * 修改排列5方法3预测
     *
     * @param caipiaoPl5MethodThree 排列5方法3预测
     * @return 结果
     */
    @Override
    public int updateCaipiaoPl5MethodThree(CaipiaoPl5MethodThree caipiaoPl5MethodThree) {
        return caipiaoPl5MethodThreeMapper.updateCaipiaoPl5MethodThree(caipiaoPl5MethodThree);
    }

    /**
     * 批量删除排列5方法3预测
     *
     * @param ids 需要删除的排列5方法3预测主键
     * @return 结果
     */
    @Override
    public int deleteCaipiaoPl5MethodThreeByIds(Long[] ids) {
        return caipiaoPl5MethodThreeMapper.deleteCaipiaoPl5MethodThreeByIds(ids);
    }

    /**
     * 删除排列5方法3预测信息
     *
     * @param id 排列5方法3预测主键
     * @return 结果
     */
    @Override
    public int deleteCaipiaoPl5MethodThreeById(Long id) {
        return caipiaoPl5MethodThreeMapper.deleteCaipiaoPl5MethodThreeById(id);
    }
}
